package com.baicaoyuan.web.controller;


import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.StringUtils;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.model.UserDO;
import com.baicaoyuan.web.request.br.BCashOutApplyRequest;
import com.baicaoyuan.web.request.br.BCashOutIdRequest;
import com.baicaoyuan.web.request.br.BCashOutPageRequest;
import com.baicaoyuan.web.request.br.BCashOutPersonPageRequest;
import com.baicaoyuan.web.response.CashOutPersonResponse;
import com.baicaoyuan.web.response.CashOutResponse;
import com.baicaoyuan.web.service.CashOutService;
import com.baicaoyuan.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "提现Controller", tags = {"提现操作接口"})
@RestController
@RequestMapping(value = "/cashOut")
public class CashOutController {


    @Autowired
    CashOutService cashOutService;
    @Autowired
    UserService userService;

    @ApiOperation(value = "申请提现（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/saveCashOutApply", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveCashOutApply(@RequestBody BCashOutApplyRequest request) {
        try {
            //防止修改源码，传入其他手机号进行申请
            UserDO user = userService.getUserByTelephone(request.getTelephone());
            if (user == null || !request.getUserId().equals(user.getId())) {
                return new Status(StateMessage.COMMIT_FAILED);
            }

            String code = userService.getVerification(request.getTelephone());
            if (code == null) {
                return new Status(StateMessage.CAPTCHA_CLICK);
            }

            if (request.getVerification().equals(code)) {
                cashOutService.save(request);
                return new Status(StateMessage.SUCCESS);
            } else {
                return new Status(StateMessage.CAPTCHA_FAILURE);
            }
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "后台-拒绝提现（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/reject", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status reject(@RequestBody BCashOutIdRequest request) {
        try {
            this.cashOutService.reject(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-获取提现记录列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getCashOutApply", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getCashOutApply(@RequestBody BCashOutPageRequest request) {
        try {
            List<CashOutResponse> user = this.cashOutService.getCashOutApplies(StringUtils.plusPercent(request.getTelephone()),
                    StringUtils.plusPercent(request.getStatus()), request.getPage(), request.getSize());
            Integer count = this.cashOutService.getCountOfAll(StringUtils.plusPercent(request.getTelephone()), StringUtils.plusPercent(request.getStatus()));
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("CashOut", user);
            result.setData("totalCount", count);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "获取个人提现申请列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getPersonCashOutApply", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getPersonCashOutApply(@RequestBody BCashOutPersonPageRequest request) {
        try {
            List<CashOutPersonResponse> list = this.cashOutService.getPersonApplies(request.getUserId(), request.getPage(), request.getSize());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("personCashOut", list);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台=提现成功（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/checkSuccess", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status checkSuccess(@RequestBody BCashOutIdRequest request) {
        try {
            this.cashOutService.checkSuccess(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }
}
